package com.springboot.demo.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.demo.core.constant.CodeMsg;
import com.springboot.demo.core.constant.Constants;
import com.springboot.demo.core.utils.SysUtils;
import com.springboot.demo.module.domain.BaseResult;
import com.springboot.demo.module.domain.vo.UserVO;
import com.springboot.demo.module.entity.TSysUser;
import com.springboot.demo.module.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/****************************************************
 *
 * 用户controller
 *
 * @author Francis
 * @date 2019-02-24 11:58
 * @version 1.0
 **************************************************/
@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 查询所有
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public Map<String, Object> getAllUser(){
        List<TSysUser> userList = userService.list(null);

        return BaseResult.ajaxReturnSuccess(userList);
    }


    /**
     * 新增
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map<String, Object> addUser(@RequestBody UserVO userVO){
        TSysUser pageUser = new TSysUser();
        BeanUtils.copyProperties(userVO, pageUser);

        Boolean flag = userService.addUser(pageUser);

        if(flag){
            return BaseResult.ajaxReturnSuccess(null);
        }else{
            return BaseResult.ajaxReturnError(CodeMsg.INSERT_ERROR);
        }
    }


    /**
     * 删除
     *
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public Map<String, Object> addUser(@PathVariable("id") String userId){
        boolean deleteFlag = false;
        TSysUser sysUser = new TSysUser();
        sysUser.setUserId(userId);
        sysUser.setState(Constants.USER_STATE_DELETE);

        try {
            deleteFlag = userService.updateById(sysUser);

        } catch (Exception e) {
            log.error("删除过程中出现异常！", e);
            return BaseResult.ajaxReturnError(CodeMsg.DB_ERROR);
        }

        if(deleteFlag){
            return BaseResult.ajaxReturnSuccess(null);
        }else{
            return BaseResult.ajaxReturnError(CodeMsg.DATA_DEAL_ERROR);
        }
    }


    /**
     * 更新
     *
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    public Map<String, Object> update(@PathVariable("id") String userId, @RequestBody UserVO userVO){
        TSysUser pageUser = new TSysUser();
        BeanUtils.copyProperties(userVO, pageUser);
        pageUser.setUserId(userId);
        pageUser.setUserPassword(SysUtils.encodeByMd5(userVO.getUserPassword()));

        boolean updateFlag = false;
        try {
            updateFlag = userService.updateById(pageUser);
        } catch (Exception e) {
            log.error("更新过程中出现异常！", e);
            return BaseResult.ajaxReturnError(CodeMsg.DB_ERROR);
        }

        if(updateFlag){
            return BaseResult.ajaxReturnSuccess(null);
        }else{
            return BaseResult.ajaxReturnError(CodeMsg.DATA_DEAL_ERROR);
        }
    }


    /**
     * 激活/锁定
     *
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH, produces = "application/json;charset=UTF-8")
    public Map<String, Object> updateState(@PathVariable("id") String userId, @RequestBody UserVO userVO){
        TSysUser pageUser = new TSysUser();
        BeanUtils.copyProperties(userVO, pageUser);
        pageUser.setUserId(userId);

        boolean updateFlag = false;
        try {
            updateFlag = userService.updateById(pageUser);
        } catch (Exception e) {
            log.error("更新过程中出现异常！", e);
            return BaseResult.ajaxReturnError(CodeMsg.DB_ERROR);
        }

        if(updateFlag){
            return BaseResult.ajaxReturnSuccess(null);
        }else{
            return BaseResult.ajaxReturnError(CodeMsg.DATA_DEAL_ERROR);
        }
    }

}
